package com.jiuzhe.commercial.ui.house.editor;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.amap.api.services.help.Tip;
import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.bean.HouseStayListBean;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.ui.house.CityListSelectActivity2;
import com.jiuzhe.commercial.ui.house.SearchResultActivity;
import com.jiuzhe.commercial.utils.RxBus;
import com.jiuzhe.commercial.utils.UIUtils;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by huanghao on 2018/6/8.
 */

public class EditorHomeStayMapLocationActivity extends BaseActivity{
    @BindView(R.id.et_city)
    TextView etCity;
    @BindView(R.id.et_area)
    TextView etArea;
    @BindView(R.id.et_room_num)
    EditText etRoomNum;
    private Tip tip;
    private String regionId;
    private String regionName;
    private String mCity,cityName,cityArea;
    private String mArea;
    private String roomNo;
    private String address;
    private Subscription mSubscription;



    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_editor_home_map_lication);
        if (null != Constants.HOUSESTAYLISTBEAN){
        cityName = Constants.HOUSESTAYLISTBEAN.getCityName();
        cityArea = Constants.HOUSESTAYLISTBEAN.getArea();
            address = Constants.HOUSESTAYLISTBEAN.getAddress();
            roomNo = Constants.HOUSESTAYLISTBEAN.getRoomNo();
            etArea.setText(address);

            etRoomNum.setText(roomNo);
            etRoomNum.setSelection(Constants.HOUSESTAYLISTBEAN.getRoomNo().length());//将光标移至文字末尾
            etCity.setText(cityName + cityArea);
        }
            setTitle("位置");
            setRringTitle("下一步");
            setOnClik();
        initData();
    }

    private void initData() {
        mSubscription = RxBus.getInstance().toObserverable(HouseStayListBean.class)
                .subscribe(new Action1<HouseStayListBean>() {
                    @Override
                    public void call(HouseStayListBean bean) {
                      finish();
                    }
                });
    }


    private void setOnClik() {
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addHouseLocation();
            }
        });
    }



    @OnClick({R.id.et_city,R.id.et_area})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.et_city:
                Intent intent = new Intent(this, CityListSelectActivity2.class);
                startActivityForResult(intent, 2);
                break;

            case R.id.et_area:
                String houseCity = etCity.getText().toString().trim();
                if (TextUtils.isEmpty(houseCity)) {
                    UIUtils.showToastSafe("请先选择添加房源城市地区！");
                    return;
                }
                Intent intent2 = new Intent(this, SearchResultActivity.class);
                if (null != mCity){
                    intent2.putExtra("city",mCity);
                    intent2.putExtra("area",mArea);
                }else {
                    intent2.putExtra("city",cityName);
                    intent2.putExtra("area",cityArea);
                }

                startActivityForResult(intent2, 0);
                break;
        }
    }


    @Override
    protected void loadData() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 0 && resultCode == 1) {
            tip = data.getParcelableExtra("tip");
            etArea.setText(tip.getAddress());
        }

        if (requestCode == 2 && resultCode == 3) {
            mCity = data.getStringExtra("city");
            mArea = data.getStringExtra("area");
            etCity.setText(mCity + mArea);
        }
    }


    private void addHouseLocation() {
        final HouseStayListBean houseStayListBean = new HouseStayListBean();
        String houseCity = etCity.getText().toString().trim();
        String houseArea = etArea.getText().toString().trim();
        String roomNum = etRoomNum.getText().toString().trim();
        if (TextUtils.isEmpty(houseCity)) {
            UIUtils.showToastSafe("请选择添加房源城市地区!");
            return;
        }
        if (TextUtils.isEmpty(houseArea)) {
            UIUtils.showToastSafe("请选择添加房源具体地址！");
            return;
        }
        if (TextUtils.isEmpty(roomNum)) {
            UIUtils.showToastSafe("请输入添加房源具体房号！");
            return;
        }
       /* HashMap<String, String> map = new HashMap<>();
        String token = SPUtil.getString(EditorHomeStayMapLocationActivity.this, Constants.TOKEN);
        String id = SPUtil.getString(EditorHomeStayMapLocationActivity.this, Constants.ID);
        String autoSignStr = Constants.SKUUPDATING.substring(Constants.SKUUPDATING.lastIndexOf("0/") + 1) + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);
        map.put("token", id);
        map.put("sign", signStr);
        map.put("skuId", Constants.HOUSESTAYLISTBEAN.getId());
        if (houseCity.equals(regionName)){
            map.put("regionId", Constants.HOUSESTAYLISTBEAN.getRegionId());
        }else {
            map.put("regionId", regionId);
        }
            map.put("address", houseArea);
            map.put("roomNo", roomNum);

        HttpClient.post(this, Constants.SKUUPDATING, map, new CallBack<String>() {
            @Override
            public void onSuccess(String result) {
                UIUtils.showToastSafe("保存成功");
                RxBus.getInstance().post(houseStayListBean);
                finish();
            }
        });*/
        Intent intent = new Intent(EditorHomeStayMapLocationActivity.this,EditorHomeLocationActivity.class);
        intent.putExtra("tip",tip);
        intent.putExtra("roomNum",roomNum);
        if (null != mCity){
            intent.putExtra("city",mCity);
            intent.putExtra("area",mArea);
        }else {
            intent.putExtra("city",cityName);
            intent.putExtra("area",cityArea);
        }
        intent.putExtra("roomNum",roomNum);
        intent.putExtra("houseArea",houseArea);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSubscription != null ) {
            if(!mSubscription.isUnsubscribed()){
                mSubscription.unsubscribe();
            }
        }
    }
}
